/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.sd.common.constants.GlobalEnum;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.service.DocRecvService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.service.DocDeliverRecordService;

/**
 * 文件复制Controller
 * 等待还文列表
 * @author ysj
 * @version 2020-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/docDeliverWaitReturn")
public class DocDeliverWaitReturnController extends BaseController {

    @Autowired
    private DocDeliverRecordService recordService;
    @Autowired
    private DocRecvService docRecvService;
    /**
     * 获取数据
     */
    @ModelAttribute
    public DocDeliverRecord get(String id, boolean isNewRecord) {
        return recordService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("sd:docDeliverWaitReturn:view")
    @RequestMapping(value = {"list", ""})
    public String list(DocDeliverRecord docDeliverRecord, Model model) {

        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverWaitReturnList";
    }
    /**
     * 等待还文首页
     */
    @RequiresPermissions("sd:docDeliverWaitReturn:view")
    @RequestMapping(value = {"index", ""})
    public String index(DocDeliverRecord docDeliverRecord, Model model) {

        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverWaitReturnIndex";
    }
    /**
     * 查询列表数据
     */
    @RequiresPermissions("sd:docDeliverWaitReturn:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DocDeliverRecord> listData(DocDeliverRecord docDeliverRecord, HttpServletRequest request, HttpServletResponse response) {
        docDeliverRecord.setPage(new Page<>(request, response));
        docDeliverRecord.getSqlMap().getWhere().and("take_date",QueryType.IS_NOT_NULL,null);
        docDeliverRecord.getSqlMap().getWhere().and("return_date",QueryType.IS_NULL,null).and("deliver_type",QueryType.NE, GlobalEnum.RECORD_DELIVER_TYPE_PROPOSED.getKey(),1).and("deliver_type",QueryType.NE,GlobalEnum.RECORD_DELIVER_TYPE_REGISTER,2);
        Page<DocDeliverRecord> page = recordService.findPage(docDeliverRecord);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("sd:docDeliverWaitReturn:view")
    @RequestMapping(value = "form")
    public String form(DocDeliverRecord docDeliverRecord, Model model) {
        String str=docDeliverRecord.getDocRecv().getId();
        DocRecv docRecv = docRecvService.get(str);

        model.addAttribute("docRecv", docRecv);
        return "modules/sd/docDeliverHistoryShow";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("sd:docDeliverWaitReturn:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated DocDeliverRecord docDeliverRecord) {
        recordService.save(docDeliverRecord);
        return renderResult(Global.TRUE, text("保存文件分发成功！"));
    }

}